package org.shuma.goods.controller;

import org.shuma.goods.domain.DTO.ProductDTO;
import org.shuma.goods.domain.DTO.ProductIdsDTO;
import org.shuma.goods.commons.Result;
import org.shuma.goods.domain.entity.ProductBasicInfo;
import org.shuma.goods.domain.entity.ProductDetailsInfo;
import org.shuma.goods.service.ProductBasicInfoService;
import org.shuma.goods.service.ProductDetailsInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    /**
     * 调用user服务
     */
    @Autowired
    private ProductBasicInfoService productBasicInfoService;
    @Autowired
    private ProductDetailsInfoService productDetailsInfoService;

    /**
     * 添加商品
     * @param productDTO
     * @return
     */
    @PostMapping
    public Result addGoods(@RequestBody ProductDTO productDTO){
        if (productDTO == null){
            return Result.success("操作失败");
        }
        ProductBasicInfo productBasicInfo = new ProductBasicInfo();
        ProductDetailsInfo productDetailsInfo = new ProductDetailsInfo();
        BeanUtils.copyProperties(productDTO,productBasicInfo);
        BeanUtils.copyProperties(productDTO,productDetailsInfo);
        ProductDetailsInfo detailsInfo = productDetailsInfoService.insert(productDetailsInfo);
        productBasicInfo.setPDetailsId(detailsInfo.getPDetailsId());
        productBasicInfoService.insert(productBasicInfo);
        return Result.success("操作成功");
    }

    /**
     * 删除商品
     * @param productIdsDTO
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteGoods(@RequestBody ProductIdsDTO productIdsDTO){
        String pids = productIdsDTO.getPids();
        if (pids.isEmpty() || pids == null){
            return Result.error("操作失败");
        }

        List<Long> productIdList = Arrays.stream(pids.split(","))
                .map(String::trim) // 去除可能的空白字符
                .map(Integer::parseInt)// 将字符串转换为整数
                .map(Integer::longValue)
                .collect(Collectors.toList());
        for (Long aLong : productIdList) {

        }
        productBasicInfoService.deleteById(productIdList);
        return Result.success("操作成功");
    }

    /**
     * 更新商品
     * @param productDTO
     * @return
     */
    @PutMapping("/update")
    public Result updateGoods(@RequestBody ProductDTO productDTO){
        ProductBasicInfo productBasicInfo = new ProductBasicInfo();
        ProductDetailsInfo productDetailsInfo = new ProductDetailsInfo();
        BeanUtils.copyProperties(productDTO,productBasicInfo);
        BeanUtils.copyProperties(productDTO,productDetailsInfo);
        try{
            productDetailsInfoService.update(productDetailsInfo);
            productBasicInfoService.update(productBasicInfo);
        }
        catch (Exception e){
            return Result.error("操作失败");
        }
        return Result.success("操作成功");
    }
}
